package com.xnyzc.lhy.mis.service.impl.cms.price;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xnyzc.lhy.common.component.redis.RedisCommon;
import com.xnyzc.lhy.common.constant.RedisPrefixConstant;
import com.xnyzc.lhy.common.entity.Qw;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.cms.EVehicleType;
import com.xnyzc.lhy.common.penum.order.EOrderChannelType;
import com.xnyzc.lhy.common.penum.vehicle.ECallVehType;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.cms.price.AddBillRuleParam;
import com.xnyzc.lhy.mis.entity.param.cms.price.AddPriceParam;
import com.xnyzc.lhy.mis.entity.param.cms.price.NewBillRulesParam;
import com.xnyzc.lhy.mis.entity.param.cms.price.PriceParam;
import com.xnyzc.lhy.mis.entity.platform.OaSysPlatformTimeControl;
import com.xnyzc.lhy.mis.entity.result.cms.price.BillRulesAndSpecialSceneRes;
import com.xnyzc.lhy.mis.entity.system.*;
import com.xnyzc.lhy.mis.mapper.system.*;
import com.xnyzc.lhy.mis.service.cms.price.PriceTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 价格模板相关
 * </p>
 *
 * @author rhr
 * @since 2020-11-15
 */
@Service
@Slf4j
public class PriceTemplateServiceImpl implements PriceTemplateService {

    @Autowired
    private OaSysBillRuleGroupMapper oaSysBillRuleGroupMapper;

    @Autowired
    private OaSysBillRuleEmbargoMapper oaSysBillRuleEmbargoMapper;

    @Autowired
    private OaSysCityProperRuleGroupMapper oaSysCityProperRuleGroupMapper;

    @Autowired
    private OaSysCityProperRuleMapper oaSysCityProperRuleMapper;

    @Autowired
    private OaSysPlatformTimeRuleMapper oaSysPlatformTimeRuleMapper;

    @Autowired
    private OaSysBillRuleMapper oaSysBillRuleMapper;

    @Autowired
    private OaSysBillSpecialTimeRuleMapper oaSysBillSpecialTimeRuleMapper;

    @Autowired
    private OaSysBillSpecialDateRuleMapper oaSysBillSpecialDateRuleMapper;


    @Autowired
    private OaSysUserMapper oaSysUserMapper;

    @Autowired
    private RedisCommon redisCommon;

    @Autowired
    private OaSysBillSpecialSceneRuleMapper oaSysBillSpecialSceneRuleMapper;

    @Override
    public Rv addOrUpdateRuleGroup(OaSysBillRuleGroup ruleGroup) {
        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)) {
            return Rv.error(EErrorCode.noAuth);
        }
        Date date = new Date();
        if (ruleGroup.getGroupId() == null) {
            ruleGroup.setGroupId(IDUtil.nextId());
            ruleGroup.setCreateUser(userId);
            ruleGroup.setUpdateUser(userId);
            ruleGroup.setCreateTime(date);
            ruleGroup.setUpdateTime(date);
            ruleGroup.setDeleted(0);
            int res = oaSysBillRuleGroupMapper.insert(ruleGroup);
            if (res <= 0) {
                return Rv.error(EErrorCode.applyAmountOperateFailure);
            }
        } else {
            ruleGroup.setUpdateUser(userId);
            ruleGroup.setUpdateTime(date);
            int res = oaSysBillRuleGroupMapper.updateById(ruleGroup);
            if (res > 0) {
                //修改日常计费规则在抽成比例
                Integer drawRatio = ruleGroup.getDrawRatio();

                //OaSysBillRule mRule = null;
                QueryWrapper<OaSysBillRule> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq(OaSysBillRule.GROUP_ID, ruleGroup.getGroupId());
                List<OaSysBillRule> rules = oaSysBillRuleMapper.selectList(queryWrapper);
                for (OaSysBillRule rule : rules) {
                    //mRule = rule;
                    rule.setDrawRatio(drawRatio);
                    oaSysBillRuleMapper.updateById(rule);
                }
                //重新发布计费组规则
                List<Integer> vehicleTypes = EVehicleType.getAllVehicleType();
                for (Integer vehicleType : vehicleTypes) {
                    EOrderChannelType[] channelTypes = EOrderChannelType.values();
                    for (EOrderChannelType channelType : channelTypes) {
                        ECallVehType[] callTypes = ECallVehType.values();
                        for (ECallVehType callType : callTypes) {
                            String key1 = RedisPrefixConstant.BILL_RULES + "_" + channelType.getType() + "_" + ruleGroup.getCityId() + "_" + vehicleType + "_" + callType.getType();
                            String key2 = RedisPrefixConstant.BILL_SPECIAL_TIME_RULES + "_" + channelType.getType() + "_" + ruleGroup.getCityId() + "_" + vehicleType + "_" + callType.getType();
                            String key3 = RedisPrefixConstant.BILL_SPECIAL_DATE_RULES + "_" + channelType.getType() + "_" + ruleGroup.getCityId() + "_" + vehicleType + "_" + callType.getType();
                            redisCommon.remove(key1);
                            redisCommon.remove(key2);
                            redisCommon.remove(key3);
                        }
                    }
                }
            } else {
                return Rv.error(EErrorCode.applyAmountOperateFailure);
            }
        }

        return Rv.wrap();
    }

    @Override
    public Rv addOrUpdateCityProperRuleGroup(OaSysCityProperRuleGroup ruleGroup) {
        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)) {
            return Rv.error(EErrorCode.noAuth);
        }
        Date date = new Date();
        if (ruleGroup.getGroupId() == null) {//添加
            ruleGroup.setGroupId(IDUtil.nextId());
            ruleGroup.setCreateUser(userId);
            ruleGroup.setUpdateUser(userId);
            ruleGroup.setCreateTime(date);
            ruleGroup.setUpdateTime(date);
            ruleGroup.setDeleted(0);
            int res = oaSysCityProperRuleGroupMapper.insert(ruleGroup);
            if (res <= 0) {
                return Rv.error(EErrorCode.applyAmountOperateFailure);
            }
        } else {//更新
            ruleGroup.setUpdateUser(userId);
            ruleGroup.setUpdateTime(date);
            int res = oaSysCityProperRuleGroupMapper.updateById(ruleGroup);
            if (res <= 0) {
                return Rv.error(EErrorCode.applyAmountOperateFailure);
            }
        }

        return Rv.wrap();
    }

    @Override
    public Rv getCityProperRuleGroup(PriceParam priceParam) {
        Qw qw = Qw.create();
        qw.orderByAsc(OaSysBillRuleGroup.CITY_ID);
        if (CheckUtil.objIsNotEmpty(priceParam.getAreaId())) {
            qw.eq(OaSysBillRuleGroup.CITY_ID, priceParam.getAreaId());
        }
        if (CheckUtil.objIsNotEmpty(priceParam.getOfficeId())) {
            qw.eq(OaSysBillRuleGroup.COMPANY_ID, priceParam.getOfficeId());
        }
        Integer page = priceParam.getPage();
        if (page == null || page < 1) {
            page = 1;
        }
        Integer pageSize = priceParam.getPageSize();
        if (pageSize == null || pageSize < 10) {
            pageSize = 10;
        }
        // 创建分页器
        IPage iPage = new Page();
        // 设置需要查询的页码
        iPage.setCurrent(page);
        // 设置每页需要显示的数据量
        iPage.setSize(pageSize);
        iPage = oaSysCityProperRuleGroupMapper.selectPage(iPage, qw);
        return Rv.wrap(iPage);
    }

    @Override
    public Rv getRuleGroup(PriceParam priceParam) {
        Long sysUserId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(sysUserId)) {
            Rv.error(EErrorCode.missingAuthArgs);
        }
        Qw qw = Qw.create();
        qw.orderByAsc(OaSysBillRuleGroup.CITY_ID);
        if (CheckUtil.objIsNotEmpty(priceParam.getAreaId())) {
            qw.eq(OaSysBillRuleGroup.CITY_ID, priceParam.getAreaId());
        }
        OaSysUser oaSysUser = oaSysUserMapper.selectById(sysUserId);
        if (CheckUtil.objIsNotEmpty(oaSysUser)) {
            Long companyId = oaSysUser.getCompanyId();
            Long officeId = oaSysUser.getOfficeId();
            if (CheckUtil.objIsNotEmpty(companyId, officeId)) {
                if (CheckUtil.objIsNotEmpty(priceParam.getOfficeId())) {
                    if (companyId == 1L && officeId == 1L) {//是所有的数据 根据查询条件查
                        qw.eq(OaSysBillRuleGroup.COMPANY_ID, priceParam.getOfficeId());
                    } else if (companyId == 1L && officeId != 1L) {//不是所有数据
                        qw.eq(OaSysBillRuleGroup.COMPANY_ID, officeId);
                    } else if (companyId != 1L && officeId != 1L) {//不是所有数据
                        qw.eq(OaSysBillRuleGroup.COMPANY_ID, companyId);
                    }
                } else {//当公司id为空时，获取当前角色可查看部门(锁权限问题)
                    //判断一下用户是不是获取所有的数据
                    if (companyId == 1L && officeId == 1L) {//是所有的数据
                        //跳过
                    } else if (companyId != 1L && officeId != 1L) {//不是所有数据
                        qw.eq(OaSysBillRuleGroup.COMPANY_ID, companyId);
                    } else if (companyId == 1L && officeId != 1L) {//不是所有数据
                        qw.eq(OaSysBillRuleGroup.COMPANY_ID, officeId);
                    }
                }
            }
        }
        Integer page = priceParam.getPage();
        if (page == null || page < 1) {
            page = 1;
        }
        Integer pageSize = priceParam.getPageSize();
        if (pageSize == null || pageSize < 10) {
            pageSize = 10;
        }
        // 创建分页器
        IPage iPage = new Page();
        // 设置需要查询的页码
        iPage.setCurrent(page);
        // 设置每页需要显示的数据量
        iPage.setSize(pageSize);
        iPage = oaSysBillRuleGroupMapper.selectPage(iPage, qw);
        return Rv.wrap(iPage);
    }

    @Override
    public Rv addOrUpdateCityProperRule(AddPriceParam priceParam) {
        Long userId = TokenUtil.getTokenUserId();
        String ruleName = priceParam.getRuleName();
        Integer callType = priceParam.getCallType();
        Integer robable = priceParam.getRobable();
        Integer premium = priceParam.getPremium();
        Integer cityId = priceParam.getAreaId();
        Integer channelId = priceParam.getChannel();
        List<AddBillRuleParam> params = priceParam.getParams();

        if (CheckUtil.objIsEmpty(userId, callType, robable, premium, ruleName, params, cityId, channelId)) {
            return Rv.error(EErrorCode.missingArg);
        }
        //验证城市规则组是否存在
        OaSysCityProperRuleGroup ruleGroup = oaSysCityProperRuleGroupMapper.selectOne(Qw.create().eq(OaSysCityProperRuleGroup.CITY_ID, cityId));
        if (ruleGroup == null) {
            return Rv.error(EErrorCode.infoIsNull);
        }

        Date currentTime = new Date();

        for (AddBillRuleParam param : params) {
            insertOrUpdateCityProperRule(userId, ruleGroup.getGroupId(), ruleName, param.getCityProperRules(), cityId, channelId, param.getVehicleType(), callType, currentTime);
        }
        return Rv.wrap();
    }

    @Override
    public Rv getCityProperRuleDetails(PriceParam priceParam) {
        Long groupId = priceParam.getGroupId();
        List<Integer> vehicleTypes = EVehicleType.getAllVehicleType();
        List<AddBillRuleParam> params = new ArrayList<>(vehicleTypes.size() * 2);
        for (Integer vehicleType : vehicleTypes) {
            AddBillRuleParam param = new AddBillRuleParam();
            param.setVehicleType(vehicleType);

            List<OaSysCityProperRule> rules = oaSysCityProperRuleMapper.selectCityProperRuleList(groupId, vehicleType, priceParam.getCallType(), priceParam.getAreaId(), priceParam.getChannel());
            param.setCityProperRules(rules);

            params.add(param);
        }

        return Rv.wrap(params);
    }

    @Override
    public Rv addOrUpdatePlatformTimeRule(AddPriceParam priceParam) {

        Long userId = TokenUtil.getTokenUserId();
        Integer cityId = priceParam.getAreaId();
        List<AddBillRuleParam> params = priceParam.getParams();

        if (CheckUtil.objIsEmpty(userId, params, cityId)) {
            return Rv.error(EErrorCode.missingArg);
        }
        //验证规则组是否存在
        OaSysBillRuleGroup ruleGroup = oaSysBillRuleGroupMapper.selectOne(Qw.create().eq(OaSysBillRuleGroup.CITY_ID, cityId));
        if (ruleGroup == null) {
            return Rv.error(EErrorCode.parentIsNull);
        }

        Date currentTime = new Date();

        for (AddBillRuleParam param : params) {
            insertOrUpdatePlatformTimeRuleToRedis(userId, ruleGroup.getGroupId(), param.getPlatformTimeControls(), cityId, currentTime);
        }
        return Rv.wrap();
    }

    @Override
    public Rv getPlatformTimeRuleDetails(PriceParam priceParam) {
        Long groupId = priceParam.getGroupId();
        Long areaId = priceParam.getAreaId();
        if (CheckUtil.objIsEmpty(groupId)) {
            return Rv.error(EErrorCode.missingArg);
        }
        QueryWrapper<OaSysPlatformTimeControl> oaSysPlatformTimeControlQueryWrapper = new QueryWrapper<>();
        oaSysPlatformTimeControlQueryWrapper.eq(OaSysPlatformTimeControl.GROUP_ID, groupId);
        oaSysPlatformTimeControlQueryWrapper.eq(OaSysPlatformTimeControl.CITY_ID, areaId);
        oaSysPlatformTimeControlQueryWrapper.eq(OaSysPlatformTimeControl.DELETED, 0);

        List<OaSysPlatformTimeControl> oaSysPlatformTimeControlList = oaSysPlatformTimeRuleMapper.selectList(oaSysPlatformTimeControlQueryWrapper);
        if (CheckUtil.objIsEmpty(oaSysPlatformTimeControlList)) {
            return Rv.error(EErrorCode.infoIsNull);
        }
        return Rv.wrap(oaSysPlatformTimeControlList);
    }

    @Override
    public Rv addOrUpdatePriceTemplate(AddPriceParam priceParam) {
        Long userId = TokenUtil.getTokenUserId();
        String ruleName = priceParam.getRuleName();
        Integer callType = priceParam.getCallType();
        Integer robable = priceParam.getRobable();
        Integer premium = priceParam.getPremium();
        Integer cityId = priceParam.getAreaId();
        Integer channelId = priceParam.getChannel();
        List<AddBillRuleParam> params = priceParam.getParams();
        //参数判空校验
        if (CheckUtil.objIsEmpty(userId, callType, robable, premium, ruleName, params, cityId, channelId)) {
            return Rv.error(EErrorCode.noData);
        }
        OaSysBillRuleGroup ruleGroup = oaSysBillRuleGroupMapper.selectOne(Qw.create().eq(OaSysBillRuleGroup.CITY_ID, cityId));
        if (ruleGroup == null) {
            return Rv.error(EErrorCode.noData);
        }

        Integer drawRatio = ruleGroup.getDrawRatio();
        Date currentTime = new Date();
        for (AddBillRuleParam param : params) {
            insertOrUpdateBillRule(userId, ruleGroup.getGroupId(), ruleName, param.getBillRules(), cityId, channelId, param.getVehicleType(), callType, drawRatio, premium, robable, currentTime);
            insertOrUpdateBillSpecialTimeRule(ruleGroup.getGroupId(), param.getSpecialTimeRules(), cityId, channelId, param.getVehicleType(), callType);
            insertOrUpdateBillSpecialDateRule(ruleGroup.getGroupId(), param.getSpecialDateRules(), cityId, channelId, param.getVehicleType(), callType);
            insertOrUpdateEmbargo(userId, param.getRuleEmbargos(), cityId, channelId, param.getVehicleType(), callType);
        }

        return Rv.wrap();
    }

    /**
     * 新增或修改禁行区域数据
     *
     * @param userId
     * @param embargos
     * @param cityId
     * @param channelId
     * @param vehicleType
     * @param callType
     * @author sunzheng
     * @time 2021-03-12
     */
    private void insertOrUpdateEmbargo(Long userId, List<OaSysBillRuleEmbargo> embargos, Integer cityId, Integer channelId, Integer vehicleType, Integer callType) {
        if (CheckUtil.collectionIsEmpty(embargos)) {
            return;
        }
        Iterator<OaSysBillRuleEmbargo> iterator = embargos.iterator();
        while (iterator.hasNext()) {
            OaSysBillRuleEmbargo newEmbargo = iterator.next();
            Long id = newEmbargo.getId();
            newEmbargo.setCallType(callType);
            newEmbargo.setChannelId(channelId);
            newEmbargo.setVehicleType(vehicleType);
            newEmbargo.setCityId(cityId);
            if (id == null) {//新增
                newEmbargo.setId(IDUtil.nextId());
                newEmbargo.setCreateTime(new Date());
                newEmbargo.setCreateUser(userId);
                oaSysBillRuleEmbargoMapper.insert(newEmbargo);
            } else {//修改
                newEmbargo.setUpdateUser(userId);
                newEmbargo.setUpdateTime(new Date());
                oaSysBillRuleEmbargoMapper.updateById(newEmbargo);
            }

        }
    }

    private void insertOrUpdateBillRule(Long userId, Long groupId, String ruleName, List<OaSysBillRule> rules, Integer cityId, Integer channelId, Integer vehicleType, Integer callType, Integer drawRatio, Integer premium, Integer robable, Date date) {

        for (OaSysBillRule rule : rules) {
            Long ruleId = rule.getRuleId();
            if (ruleId == null) {
                OaSysBillRule newRule = new OaSysBillRule();
                newRule.setRuleId(IDUtil.nextId());
                newRule.setCityId(cityId);
                newRule.setChannelId(channelId);
                newRule.setVehicleType(vehicleType);
                newRule.setCallType(callType);
                newRule.setGroupId(groupId);
                newRule.setRulesName(ruleName);
                newRule.setDrawRatio(drawRatio);
                newRule.setPremium(premium);
                newRule.setRobable(robable);
                newRule.setCreateTime(date);
                newRule.setCreateUser(userId);
                newRule.setUpdateUser(userId);
                newRule.setUpdateTime(date);
                newRule.setStartFee(rule.getStartFee());
                newRule.setOtherFee(rule.getOtherFee());
                newRule.setMileageFee(rule.getMileageFee());
                newRule.setStartingMileage(rule.getStartingMileage());
                newRule.setStartMileage(rule.getStartMileage());
                newRule.setEndMileage(rule.getEndMileage());
                newRule.setDeleted(0);

                oaSysBillRuleMapper.insert(newRule);
            } else {
                OaSysBillRule newRule = new OaSysBillRule();
                newRule.setRuleId(ruleId);
                newRule.setCityId(cityId);
                newRule.setChannelId(channelId);
                newRule.setVehicleType(vehicleType);
                newRule.setCallType(callType);
                newRule.setGroupId(groupId);
                newRule.setRulesName(ruleName);
                newRule.setDrawRatio(drawRatio);
                newRule.setPremium(premium);
                newRule.setRobable(robable);
                newRule.setCreateUser(userId);
                newRule.setUpdateUser(userId);
                newRule.setUpdateTime(date);
                newRule.setStartFee(rule.getStartFee());
                newRule.setOtherFee(rule.getOtherFee());
                newRule.setMileageFee(rule.getMileageFee());
                newRule.setStartingMileage(rule.getStartingMileage());
                newRule.setStartMileage(rule.getStartMileage());
                newRule.setEndMileage(rule.getEndMileage());
                newRule.setDeleted(rule.getDeleted());

                oaSysBillRuleMapper.updateById(newRule);
            }
        }
    }

    private void insertOrUpdateCityProperRule(Long userId, Long groupId, String ruleName, List<OaSysCityProperRule> rules, Integer cityId, Integer channelId, Integer vehicleType, Integer callType, Date date) {

        for (OaSysCityProperRule rule : rules) {
            Long ruleId = rule.getRuleId();
            if (ruleId == null) {//添加
                OaSysCityProperRule newRule = new OaSysCityProperRule();
                newRule.setRuleId(IDUtil.nextId());
                newRule.setCityId(cityId);
                newRule.setChannelId(channelId);
                newRule.setVehicleType(vehicleType);
                newRule.setCallType(callType);
                newRule.setGroupId(groupId);
                newRule.setRulesName(ruleName);
                newRule.setCreateTime(date);
                newRule.setCreateUser(userId);
                newRule.setUpdateUser(userId);
                newRule.setUpdateTime(date);
                newRule.setStartFee(rule.getStartFee());
                newRule.setOtherFee(rule.getOtherFee());
                newRule.setMileageFee(rule.getMileageFee());
                newRule.setStartingMileage(rule.getStartingMileage());
                newRule.setStartMileage(rule.getStartMileage());
                newRule.setEndMileage(rule.getEndMileage());
                newRule.setDeleted(0);

                oaSysCityProperRuleMapper.insert(newRule);
            } else {//更新
                OaSysCityProperRule newRule = new OaSysCityProperRule();
                newRule.setRuleId(ruleId);
                newRule.setCityId(cityId);
                newRule.setChannelId(channelId);
                newRule.setVehicleType(vehicleType);
                newRule.setCallType(callType);
                newRule.setGroupId(groupId);
                newRule.setRulesName(ruleName);
                newRule.setCreateTime(date);
                newRule.setCreateUser(userId);
                newRule.setUpdateUser(userId);
                newRule.setUpdateTime(date);
                newRule.setStartFee(rule.getStartFee());
                newRule.setOtherFee(rule.getOtherFee());
                newRule.setMileageFee(rule.getMileageFee());
                newRule.setStartingMileage(rule.getStartingMileage());
                newRule.setStartMileage(rule.getStartMileage());
                newRule.setEndMileage(rule.getEndMileage());
                newRule.setDeleted(rule.getDeleted());

                oaSysCityProperRuleMapper.updateById(newRule);
            }
        }
    }

    private void insertOrUpdatePlatformTimeRule(Long userId, Long groupId, List<OaSysPlatformTimeControl> rules, Integer cityId, Date date) {

        for (OaSysPlatformTimeControl rule : rules) {
            Long ruleId = rule.getControlId();
            if (ruleId == null) {//添加
                //验证是否存在
                QueryWrapper<OaSysPlatformTimeControl> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq(OaSysPlatformTimeControl.CHANNEL_ID, rule.getChannelId());
                queryWrapper.eq(OaSysPlatformTimeControl.CITY_ID, cityId);
                queryWrapper.eq(OaSysPlatformTimeControl.DELETED, 0);
                List<OaSysPlatformTimeControl> controlList = oaSysPlatformTimeRuleMapper.selectList(queryWrapper);
                if (controlList.size() > 0) {
                    log.info("平台时间控制-创建城市已存在{}", cityId);
                    continue;
                }
                OaSysPlatformTimeControl newRule = new OaSysPlatformTimeControl();
                newRule.setControlId(IDUtil.nextId());
                newRule.setCityId(cityId);
                newRule.setChannelId(rule.getChannelId());
                newRule.setGroupId(groupId);
                newRule.setCompanyId(rule.getCompanyId());
                newRule.setChannelName(rule.getChannelName());
                newRule.setUseFullTime(rule.getUseFullTime());
                newRule.setStartTime(rule.getStartTime());
                newRule.setEndTime(rule.getEndTime());
                newRule.setDelayTime(rule.getDelayTime());
                newRule.setCreateTime(date.getTime());
                newRule.setCreateUser(userId);
                newRule.setUpdateUser(userId);
                newRule.setUpdateTime(date.getTime());
                newRule.setDeleted(0);

                oaSysPlatformTimeRuleMapper.insert(newRule);
            } else {//更新
                OaSysPlatformTimeControl newRule = new OaSysPlatformTimeControl();
                newRule.setControlId(ruleId);
                newRule.setCityId(cityId);
                newRule.setChannelId(rule.getChannelId());
                newRule.setGroupId(groupId);
                newRule.setCompanyId(rule.getCompanyId());
                newRule.setChannelName(rule.getChannelName());
                newRule.setUseFullTime(rule.getUseFullTime());
                newRule.setStartTime(rule.getStartTime());
                newRule.setEndTime(rule.getEndTime());
                newRule.setDelayTime(rule.getDelayTime());
                newRule.setUpdateUser(userId);
                newRule.setUpdateTime(date.getTime());
                newRule.setDeleted(rule.getDeleted());

                oaSysPlatformTimeRuleMapper.updateById(newRule);
            }
        }
    }

    private void insertOrUpdatePlatformTimeRuleToRedis(Long userId, Long groupId, List<OaSysPlatformTimeControl> rules, Integer cityId, Date date) {

        for (OaSysPlatformTimeControl rule : rules) {
            Long ruleId = rule.getControlId();
            if (ruleId == null) {//添加
                //验证是否存在
                QueryWrapper<OaSysPlatformTimeControl> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq(OaSysPlatformTimeControl.CHANNEL_ID, rule.getChannelId());
                queryWrapper.eq(OaSysPlatformTimeControl.CITY_ID, cityId);
                queryWrapper.eq(OaSysPlatformTimeControl.DELETED, 0);
                List<OaSysPlatformTimeControl> controlList = oaSysPlatformTimeRuleMapper.selectList(queryWrapper);
                if (controlList.size() > 0) {
                    log.info("平台时间控制-创建城市已存在{}", cityId);
                    continue;
                }
                OaSysPlatformTimeControl newRule = new OaSysPlatformTimeControl();
                newRule.setControlId(IDUtil.nextId());
                newRule.setCityId(cityId);
                newRule.setChannelId(rule.getChannelId());
                newRule.setGroupId(groupId);
                newRule.setCompanyId(rule.getCompanyId());
                newRule.setChannelName(rule.getChannelName());
                newRule.setUseFullTime(rule.getUseFullTime());
                newRule.setStartTime(rule.getStartTime());
                newRule.setEndTime(rule.getEndTime());
                newRule.setDelayTime(rule.getDelayTime());
                newRule.setCreateTime(date.getTime());
                newRule.setCreateUser(userId);
                newRule.setUpdateUser(userId);
                newRule.setUpdateTime(date.getTime());
                newRule.setDeleted(0);
                redisCommon.set(RedisPrefixConstant.PLATFORM_TIME_RULE + rule.getChannelId() + "_" + cityId, JSON.toJSONString(newRule));
                oaSysPlatformTimeRuleMapper.insert(newRule);
            } else {//更新
                redisCommon.remove(RedisPrefixConstant.PLATFORM_TIME_RULE + rule.getChannelId() + "_" + cityId);
                OaSysPlatformTimeControl newRule = new OaSysPlatformTimeControl();
                newRule.setControlId(ruleId);
                newRule.setCityId(cityId);
                newRule.setChannelId(rule.getChannelId());
                newRule.setGroupId(groupId);
                newRule.setCompanyId(rule.getCompanyId());
                newRule.setChannelName(rule.getChannelName());
                newRule.setUseFullTime(rule.getUseFullTime());
                newRule.setStartTime(rule.getStartTime());
                newRule.setEndTime(rule.getEndTime());
                newRule.setDelayTime(rule.getDelayTime());
                newRule.setUpdateUser(userId);
                newRule.setUpdateTime(date.getTime());
                newRule.setDeleted(rule.getDeleted());
                redisCommon.set(RedisPrefixConstant.PLATFORM_TIME_RULE + rule.getChannelId() + "_" + cityId, JSON.toJSONString(newRule));
                oaSysPlatformTimeRuleMapper.updateById(newRule);
            }
        }
    }


    private void insertOrUpdateBillSpecialTimeRule(Long groupId, List<OaSysBillSpecialTimeRule> specialTimeRules, Integer cityId, Integer channelId, Integer vehicleType, Integer callType) {
        for (OaSysBillSpecialTimeRule rule : specialTimeRules) {
            Long specialRuleId = rule.getSpecialRuleId();
            if (specialRuleId == null || specialRuleId <= 0) {
                OaSysBillSpecialTimeRule newRule = new OaSysBillSpecialTimeRule();
                newRule.setSpecialRuleId(IDUtil.nextId());
                newRule.setGroupId(groupId);
                newRule.setCityId(cityId);
                newRule.setChannelId(channelId);
                newRule.setVehicleType(vehicleType);
                newRule.setCallType(callType);
                newRule.setProportion(rule.getProportion());
                newRule.setStartTime(rule.getStartTime());
                newRule.setEndTime(rule.getEndTime());
                newRule.setDeleted(0);

                oaSysBillSpecialTimeRuleMapper.insert(newRule);
            } else {
                OaSysBillSpecialTimeRule newRule = new OaSysBillSpecialTimeRule();
                newRule.setSpecialRuleId(rule.getSpecialRuleId());
                newRule.setGroupId(groupId);
                newRule.setCityId(cityId);
                newRule.setChannelId(channelId);
                newRule.setVehicleType(vehicleType);
                newRule.setCallType(callType);
                newRule.setProportion(rule.getProportion());
                newRule.setDeleted(rule.getDeleted());
                newRule.setStartTime(rule.getStartTime());
                newRule.setEndTime(rule.getEndTime());
                oaSysBillSpecialTimeRuleMapper.updateById(newRule);
            }
        }
    }

    private void insertOrUpdateBillSpecialDateRule(Long groupId, List<OaSysBillSpecialDateRule> specialDateRules, Integer cityId, Integer channelId, Integer vehicleType, Integer callType) {
        for (OaSysBillSpecialDateRule rule : specialDateRules) {
            Long specialRuleId = rule.getSpecialRuleId();
            if (specialRuleId == null || specialRuleId <= 0) {
                OaSysBillSpecialDateRule newRule = new OaSysBillSpecialDateRule();
                newRule.setSpecialRuleId(IDUtil.nextId());
                newRule.setGroupId(groupId);
                newRule.setCityId(cityId);
                newRule.setChannelId(channelId);
                newRule.setVehicleType(vehicleType);
                newRule.setCallType(callType);
                newRule.setProportion(rule.getProportion());
                newRule.setStartTime(rule.getStartTime());
                newRule.setEndTime(rule.getEndTime());
                newRule.setDeleted(0);
                oaSysBillSpecialDateRuleMapper.insert(newRule);
            } else {
                OaSysBillSpecialDateRule newRule = new OaSysBillSpecialDateRule();
                newRule.setSpecialRuleId(rule.getSpecialRuleId());
                newRule.setGroupId(groupId);
                newRule.setCityId(cityId);
                newRule.setChannelId(channelId);
                newRule.setVehicleType(vehicleType);
                newRule.setCallType(callType);
                newRule.setProportion(rule.getProportion());
                newRule.setDeleted(rule.getDeleted());
                newRule.setStartTime(rule.getStartTime());
                newRule.setEndTime(rule.getEndTime());
                oaSysBillSpecialDateRuleMapper.updateById(newRule);
            }
        }
    }

    @Override
    public Rv getNewPriceTemplateDetails(PriceParam priceParam) {
        Long groupId = priceParam.getGroupId();
        List<Integer> vehicleTypes = EVehicleType.getAllVehicleType();//获取四中车辆类型，经济，舒适，商务，豪华
        List<AddBillRuleParam> params = new ArrayList<>(vehicleTypes.size() * 2);

        //通过循环四种车型，组装返回数据
        for (Integer vehicleType : vehicleTypes) {
            AddBillRuleParam param = new AddBillRuleParam();
            param.setVehicleType(vehicleType);

//            Qw ruleQw = Qw.create().eq(OaSysBillRule.GROUP_ID, groupId).eq(OaSysBillRule.VEHICLE_TYPE, vehicleType);
//            ruleQw.orderByAsc(OaSysBillRule.START_MILEAGE);
//            ruleQw.orderByAsc(OaSysBillRule.VEHICLE_TYPE);
            //查询计费基础数据
            List<OaSysBillRule> rules = oaSysBillRuleMapper.selectRuleList(groupId, vehicleType, priceParam.getCallType(), priceParam.getAreaId(), priceParam.getChannel());
            param.setBillRules(rules);

//            Qw timeQw = Qw.create().eq(OaSysBillSpecialTimeRule.GROUP_ID, groupId).eq(OaSysBillRule.VEHICLE_TYPE, vehicleType);
//            timeQw.orderByAsc(OaSysBillSpecialTimeRule.START_TIME);
//            timeQw.orderByAsc(OaSysBillSpecialDateRule.VEHICLE_TYPE);
            //查询计费特殊时间段数据
            List<OaSysBillSpecialTimeRule> specialTimeRules = oaSysBillSpecialTimeRuleMapper.selectTimeRuleList(groupId, vehicleType, priceParam.getCallType(), priceParam.getAreaId(), priceParam.getChannel());
            param.setSpecialTimeRules(specialTimeRules);

//            Qw dateQw = Qw.create().eq(OaSysBillSpecialDateRule.GROUP_ID, groupId).eq(OaSysBillRule.VEHICLE_TYPE, vehicleType);
//            dateQw.orderByAsc(OaSysBillSpecialDateRule.START_TIME);
//            timeQw.orderByAsc(OaSysBillSpecialDateRule.VEHICLE_TYPE);
            //查询计费特殊日期数据
            List<OaSysBillSpecialDateRule> specialDateRules = oaSysBillSpecialDateRuleMapper.selectDateRuleList(groupId, vehicleType, priceParam.getCallType(), priceParam.getAreaId(), priceParam.getChannel());
            param.setSpecialDateRules(specialDateRules);
            /**
             * update by: sun zheng ;time: 2021-03-12
             */
            List<OaSysBillRuleEmbargo> embargos = oaSysBillRuleEmbargoMapper.selectEmbargoList(vehicleType, priceParam.getCallType(), priceParam.getAreaId(), priceParam.getChannel());
            param.setRuleEmbargos(embargos);

            params.add(param);
        }


        return Rv.wrap(params);
    }


    @Override
    public Rv disablePrice(PriceParam priceParam) {
        Long groupId = priceParam.getGroupId();
        if (CheckUtil.objIsEmpty(groupId)) {
            return Rv.error(EErrorCode.noData);
        }
        //根据groupID查询billRules
        QueryWrapper<OaSysBillRule> billRuleQueryWrapper = new QueryWrapper<>();
        billRuleQueryWrapper.eq(OaSysBillRule.GROUP_ID, priceParam.getGroupId());
        billRuleQueryWrapper.eq(OaSysBillRule.DELETED, 0);
        List<OaSysBillRule> oaSysBillRules = oaSysBillRuleMapper.selectList(billRuleQueryWrapper);
        OaSysBillRuleGroup ruleGroup = oaSysBillRuleGroupMapper.selectOne(Qw.create().eq(OaSysBillRuleGroup.GROUP_ID, groupId));
        if (ruleGroup == null && oaSysBillRules == null) {
            return Rv.error(EErrorCode.noData);
        }
        //重新发布计费组规则
        List<Integer> vehicleTypes = EVehicleType.getAllVehicleType();
        //RedisPrefixConstant.BILL_RULES + "_" + channel + "_" + cityCode + "_" + callType
        EOrderChannelType[] channelTypes = EOrderChannelType.values();
        for (EOrderChannelType channelType : channelTypes) {
            ECallVehType[] callTypes = ECallVehType.values();
            for (ECallVehType callType : callTypes) {
                for (OaSysBillRule oaSysBillRule : oaSysBillRules) {
                    /*String key1 = RedisPrefixConstant.BILL_RULES + "_" + channelType.getType() + "_" + ruleGroup.getCityId() + "_" + vehicleType + "_" + callType.getType();
                    String key2 = RedisPrefixConstant.BILL_SPECIAL_TIME_RULES + "_" + channelType.getType() + "_" + ruleGroup.getCityId() + "_" + vehicleType + "_" + callType.getType();
                    String key3 = RedisPrefixConstant.BILL_SPECIAL_DATE_RULES + "_" + channelType.getType() + "_" + ruleGroup.getCityId() + "_" + vehicleType + "_" + callType.getType();
                    //key4 移除禁运区域数据
                    String key4 = RedisPrefixConstant.BILL_SPECIAL_EMBARGO_AREA + "_" + channelType.getType() + "_" + ruleGroup.getCityId() + "_" + "_" + callType.getType();*/
                    String key1 = RedisPrefixConstant.BILL_RULES_CHARTERED + "_" + channelType.getType() + "_" + ruleGroup.getCityId();
                    String key5 = RedisPrefixConstant.BILL_RULES + "_" + channelType.getType() + "_" + ruleGroup.getCityId() + "_" + callType.getType() + "_" + oaSysBillRule.getRulesNo();
                    /*redisCommon.remove(key1);
                    redisCommon.remove(key2);
                    redisCommon.remove(key3);
                    redisCommon.remove(key4);*/
                    redisCommon.remove(key1);
                    redisCommon.remove(key5);
                }
            }
        }
        return Rv.wrap();
    }

    /**
     * 合并表
     *
     * @return
     */
    @Override
    public Rv merageTables() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<OaSysBillRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaSysBillRule.DELETED, 0);
        queryWrapper.groupBy(OaSysBillRule.GROUP_ID, OaSysBillRule.CITY_ID, OaSysBillRule.CHANNEL_ID, OaSysBillRule.CALL_TYPE);
        List<OaSysBillRule> oaSysBillRules = oaSysBillRuleMapper.rules();
        //获取四中车辆类型，经济，舒适，商务，豪华
        List<Integer> vehicleTypes = EVehicleType.getAllVehicleType();
        int count = 0;
        for (int i = 0; i < oaSysBillRules.size(); i++) {
            OaSysBillRule oaSysBillRule = oaSysBillRules.get(i);
            List<RulesParam> rulesParamList = new ArrayList<>();
            for (Integer vehicleType : vehicleTypes) {
                Long groupId = oaSysBillRule.getGroupId();
                Integer callType = oaSysBillRule.getCallType();
                Long cityId = oaSysBillRule.getCityId().longValue();
                Integer channelId = oaSysBillRule.getChannelId();
                RulesParam rulesParam = new RulesParam();
                rulesParam.setVehicleType(vehicleType);
                //基础计费规则
                List<OaSysBillRule> rules = oaSysBillRuleMapper.selectRuleList(groupId, vehicleType, callType, cityId, channelId);
                count += rules.size();
                List<OaSysBillRuleMini> rulesMini = new ArrayList<>();

                for (int j = 0; j < rules.size(); j++) {
                    OaSysBillRuleMini mini = new OaSysBillRuleMini();
                    mini.setStartingMileage(rules.get(j).getStartingMileage());
                    mini.setStartMileage(rules.get(j).getStartMileage());
                    mini.setEndMileage(rules.get(j).getEndMileage());
                    mini.setStartFee(rules.get(j).getStartFee());
                    mini.setMileageFee(rules.get(j).getMileageFee());
                    mini.setOtherFee(rules.get(j).getOtherFee());
                    rulesMini.add(mini);
                }
                rulesParam.setBillRules(rulesMini);
                if (CheckUtil.collectionIsEmpty(rules)) {
                    //如果为空说明没有配置这个车型的计价规则
                    continue;
                }
                oaSysBillRule.setDrawRatio(rules.get(0).getDrawRatio());
                oaSysBillRule.setRulesName(rules.get(0).getRulesName());
                //禁行区域
                List<OaSysBillRuleEmbargo> embargos = oaSysBillRuleEmbargoMapper.selectEmbargoList(vehicleType, callType, cityId, channelId);
                List<OaSysBillRuleNewEmbargoMini> embargosMini = new ArrayList<>();
                for (int j = 0; j < embargos.size(); j++) {
                    OaSysBillRuleNewEmbargoMini embargoMini = new OaSysBillRuleNewEmbargoMini();
                    embargoMini.setAreaLevel(embargos.get(j).getAreaLevel());
                    embargoMini.setAreaName(embargos.get(j).getAreaName());
                    embargoMini.setAreaCode(embargos.get(j).getAreaCode());
                    embargoMini.setRefuseType(embargos.get(j).getRefuseType());
                    embargoMini.setEmbargoType(embargos.get(j).getEmbargoType());
                    embargosMini.add(embargoMini);
                }
                rulesParam.setRuleEmbargos(embargosMini);
                //查询计费特殊时间段数据
                List<SpecialRulesParam> specialRulesParamList = new ArrayList<>();
                List<OaSysBillSpecialTimeRule> specialTimeRules = oaSysBillSpecialTimeRuleMapper.selectTimeRuleList(groupId, vehicleType, callType, cityId, channelId);
                for (int j = 0; j < specialTimeRules.size(); j++) {
                    OaSysBillSpecialTimeRule oaSysBillSpecialTimeRule = specialTimeRules.get(j);
                    BillSpecialRule billSpecialRule = new BillSpecialRule();
                    billSpecialRule.setStartMileage(new BigDecimal(0));
                    billSpecialRule.setEndMileage(new BigDecimal(9999));
                    billSpecialRule.setProportion(oaSysBillSpecialTimeRule.getProportion());
                    //billSpecialRule.setStartTime(oaSysBillSpecialTimeRule.getStartTime());
                    //billSpecialRule.setEndTime(oaSysBillSpecialTimeRule.getEndTime());
                    List<BillSpecialRule> specialRules = new ArrayList<>();
                    specialRules.add(billSpecialRule);
                    SpecialRulesParam specialRulesParam = new SpecialRulesParam();
                    specialRulesParam.setOaSysBillSpecialRuleList(specialRules);
                    specialRulesParam.setTimeKey(oaSysBillSpecialTimeRule.getStartTime() + "," + oaSysBillSpecialTimeRule.getEndTime());
                    specialRulesParamList.add(specialRulesParam);
                }
                rulesParam.setSpecialTimeRules(specialRulesParamList);
                //查询计费特殊日期数据
                List<OaSysBillSpecialDateRule> specialDateRules = oaSysBillSpecialDateRuleMapper.selectDateRuleList(groupId, vehicleType, callType, cityId, channelId);
                List<SpecialRulesParam> specialDataRulesParamList = new ArrayList<>();
                for (int j = 0; j < specialDateRules.size(); j++) {
                    OaSysBillSpecialDateRule oaSysBillSpecialDateRule = specialDateRules.get(j);
                    BillSpecialRule billSpecialRule = new BillSpecialRule();
                    billSpecialRule.setStartMileage(new BigDecimal(0));
                    billSpecialRule.setEndMileage(new BigDecimal(9999));
                    billSpecialRule.setProportion(oaSysBillSpecialDateRule.getProportion());
                    // billSpecialRule.setStartTime(oaSysBillSpecialDateRule.getStartTime().toString());
                    // billSpecialRule.setEndTime(oaSysBillSpecialDateRule.getEndTime().toString());
                    List<BillSpecialRule> specialRules = new ArrayList<>();
                    specialRules.add(billSpecialRule);
                    SpecialRulesParam specialRulesParam = new SpecialRulesParam();
                    specialRulesParam.setOaSysBillSpecialRuleList(specialRules);
                    specialRulesParam.setTimeKey(sdf.format(oaSysBillSpecialDateRule.getStartTime()) + "," + sdf.format(oaSysBillSpecialDateRule.getEndTime()));
                    specialDataRulesParamList.add(specialRulesParam);
                }
                rulesParam.setSpecialDateRules(specialDataRulesParamList);
                rulesParamList.add(rulesParam);
            }
            String jsonDataRules = JSON.toJSONString(rulesParamList);
            System.out.println(jsonDataRules);
            oaSysBillRule.setJsonRules(jsonDataRules);
            oaSysBillRule.setRuleId(IDUtil.nextId());
            oaSysBillRuleMapper.insert(oaSysBillRule);
        }
        System.out.println("共处理 " + count + " 条数据。");
        return Rv.wrap(EErrorCode.success);
    }

    /**
     * 新版本查询费用设置
     *
     * @param priceParam
     * @return
     */
    @Override
    public Rv getNewPriceDetails(PriceParam priceParam) {
        List<OaSysBillRule> oaSysBillRuleList = null;
        if (CheckUtil.objIsEmpty(priceParam.getRulesNo()) || priceParam.getRulesNo().toString() == "" ||
                priceParam.getRulesNo().toString() == "-1" || priceParam.getRulesNo() == -1 || null == priceParam.getRulesNo()) {
            oaSysBillRuleList = oaSysBillRuleMapper.selectRuleListNew(
                    priceParam.getGroupId(), priceParam.getCallType(),
                    priceParam.getAreaId(), priceParam.getChannel(),
                    priceParam.getGetHistory(), priceParam.getVehicleType(), priceParam.getRuleName());
        } else {
            oaSysBillRuleList = oaSysBillRuleMapper.selectRuleListNewAndScene(priceParam.getGroupId(), priceParam.getCallType(),
                    priceParam.getAreaId(), priceParam.getChannel(), priceParam.getGetHistory(), priceParam.getRulesNo());

        }
        return Rv.wrap(EErrorCode.success, oaSysBillRuleList);
    }

    /**
     * 新版本添加费用模板
     *
     * @param billRulesParam
     * @return
     */
    @Override
    @Transactional
    public Rv addNewPriceDetails(NewBillRulesParam billRulesParam) {
        Long ruleId = billRulesParam.getRuleId();
        if (ruleId != null) {
            //规则id不为空说明要修改,把之前的数据设置为已删除。再新增一条
            OaSysBillRule oaSysBillRule = new OaSysBillRule();
            oaSysBillRule.setRuleId(ruleId);
            oaSysBillRule.setDeleted(1);
            oaSysBillRuleMapper.updateById(oaSysBillRule);
        } else {
            //双重验证，防止id没传过来
            QueryWrapper<OaSysBillRule> queryWrapper = new QueryWrapper();
            queryWrapper.eq(OaSysBillRule.CHANNEL_ID, billRulesParam.getChannel());
            queryWrapper.eq(OaSysBillRule.CALL_TYPE, billRulesParam.getCallType());
            queryWrapper.eq(OaSysBillRule.CITY_ID, billRulesParam.getAreaId());
            queryWrapper.eq(OaSysBillRule.GROUP_ID, billRulesParam.getGroupId());
            queryWrapper.eq(OaSysBillRule.DELETED, 0);
            if (null == billRulesParam.getRulesNo()) {
                queryWrapper.isNull(OaSysBillRule.RULES_NO);
            } else {
                queryWrapper.eq(OaSysBillRule.RULES_NO, billRulesParam.getRulesNo());
            }
            if (billRulesParam.getCallType() == 7) {
                queryWrapper.eq(OaSysBillRule.RULES_NAME, billRulesParam.getRuleName());
            }
            List<OaSysBillRule> oaSysBillRules = oaSysBillRuleMapper.selectList(queryWrapper);
            if (oaSysBillRules.size() > 0) {
                Iterator<OaSysBillRule> sysBillRulesIterator = oaSysBillRules.iterator();
                while (sysBillRulesIterator.hasNext()) {
                    OaSysBillRule oaSysBillRule = new OaSysBillRule();
                    oaSysBillRule.setRuleId(sysBillRulesIterator.next().getRuleId());
                    oaSysBillRule.setDeleted(1);
                    oaSysBillRuleMapper.updateById(oaSysBillRule);
                }
            }

        }
        Integer cityId = billRulesParam.getAreaId();
        OaSysBillRuleGroup ruleGroup = oaSysBillRuleGroupMapper.selectOne(Qw.create().eq(OaSysBillRuleGroup.CITY_ID, cityId));
        if (ruleGroup == null) {
            return Rv.error(EErrorCode.noData);
        }

        Integer drawRatio = ruleGroup.getDrawRatio();
        OaSysBillRule oaSysBillRule = new OaSysBillRule();
        oaSysBillRule.setRuleId(IDUtil.nextId());
        oaSysBillRule.setDrawRatio(drawRatio);
        oaSysBillRule.setGroupId(billRulesParam.getGroupId());
        oaSysBillRule.setCityId(billRulesParam.getAreaId());
        oaSysBillRule.setChannelId(billRulesParam.getChannel());
        oaSysBillRule.setRobable(billRulesParam.getRobable());
        oaSysBillRule.setCallType(billRulesParam.getCallType());
        oaSysBillRule.setVehicleType(billRulesParam.getVehicleType());
        oaSysBillRule.setCreateUser(TokenUtil.getTokenUserId());
        oaSysBillRule.setDeleted(0);
        oaSysBillRule.setCreateTime(new Date());
        oaSysBillRule.setUpdateUser(TokenUtil.getTokenUserId());
        oaSysBillRule.setUpdateTime(new Date());
        oaSysBillRule.setRulesName(billRulesParam.getRuleName());
        oaSysBillRule.setPremium(billRulesParam.getPremium());
        oaSysBillRule.setRulesNo(billRulesParam.getRulesNo());
        String fee = null;
        if (billRulesParam.getCallType() == 7) {
            CharterRulesParam charterFees = billRulesParam.getCharterFees();
            if (CheckUtil.objIsEmpty(charterFees)) {
                return Rv.wrap(EErrorCode.unData);
            }
            fee = JSON.toJSONString(charterFees);
        } else {
            List<RulesParam> fees = billRulesParam.getFees();
            if (CheckUtil.objIsEmpty(fees)) {
                return Rv.wrap(EErrorCode.unData);
            }
            fee = JSON.toJSONString(fees);
        }
        if (CheckUtil.strIsEmpty(fee)) {
            return Rv.wrap(EErrorCode.unData);
        }
        oaSysBillRule.setJsonRules(fee);
        int insert = oaSysBillRuleMapper.insert(oaSysBillRule);
        if (insert != 1) {
            return Rv.error(EErrorCode.addError);
        }
        //String key= RedisPrefixConstant.BILL_RULES + "_" +billRulesParam.getChannel()  + "_" +billRulesParam.getAreaId()  + "_" + billRulesParam.getCallType();
        //redisCommon.remove(key);
        //ArrayList<OaSysBillRule> list=new ArrayList<OaSysBillRule>();
        //list.add(oaSysBillRule);
        //redisCommon.set(key,JSON.toJSONString(list));
        return Rv.wrap(EErrorCode.success);
    }

    @Override
    public Rv exchangeRefuseArea() {
        //数据库中拒绝服务的新旧数据字段的转换
        //1.从数据库中拿到老数据
        List<OaSysBillRule> oaSysBillRuleList = oaSysBillRuleMapper.selectOldRuleList();
        if (oaSysBillRuleList.size() > 0) {
            for (OaSysBillRule oaSysBillRule : oaSysBillRuleList
            ) {
                String jsonRules = oaSysBillRule.getJsonRules();
                Long ruleId = oaSysBillRule.getRuleId();
                List<OldRulesParam> jsonObject = JSON.parseArray(jsonRules, OldRulesParam.class);
                List<RulesParam> newBillRulesParamsList = new ArrayList<>();
                for (int i = 0; i < jsonObject.size(); i++) {
                    List<OaSysBillRuleEmbargoMini> ruleEmbargos = jsonObject.get(i).getRuleEmbargos();
                    RulesParam rulesParam = new RulesParam();
                    List<OaSysBillRuleNewEmbargoMini> newRuleEmbargos = new ArrayList<>();
                    if (ruleEmbargos.size() > 0) {
                        int refuseType = 4;
                        for (OaSysBillRuleEmbargoMini oasysEm : ruleEmbargos
                        ) {
                            OaSysBillRuleNewEmbargoMini newEmbargoMini = new OaSysBillRuleNewEmbargoMini();
                            newEmbargoMini.setAreaCode(oasysEm.getAreaCode());
                            newEmbargoMini.setAreaLevel(oasysEm.getAreaLevel());
                            newEmbargoMini.setAreaName(oasysEm.getAreaName());
                            newEmbargoMini.setEmbargoType(oasysEm.getEmbargoType());

                            int startEmb = oasysEm.getStartEmb();
                            int endEmb = oasysEm.getEndEmb();

                            if (startEmb == 1 && endEmb == 0) {
                                //起点在其中拒绝
                                refuseType = 0;
                            } else if (startEmb == 0 && endEmb == 1) {
                                //终点在其中拒绝
                                refuseType = 1;
                            } else if (startEmb == 1 && endEmb == 1) {
                                //终点和起点在其中拒绝
                                refuseType = 3;
                            } else if (startEmb == 0 && endEmb == 0) {
                                //无判定
                                refuseType = 4;
                            }
                            newEmbargoMini.setRefuseType(refuseType);
                            newRuleEmbargos.add(newEmbargoMini);
                        }
                    }
                    OldRulesParam oldBillRules = jsonObject.get(i);
                    rulesParam.setBillRules(oldBillRules.getBillRules());
                    rulesParam.setCityProperRules(oldBillRules.getCityProperRules());
                    rulesParam.setPlatformTimeControls(oldBillRules.getPlatformTimeControls());
                    rulesParam.setSpecialDateRules(oldBillRules.getSpecialDateRules());
                    rulesParam.setSpecialTimeRules(oldBillRules.getSpecialTimeRules());
                    rulesParam.setVehicleType(oldBillRules.getVehicleType());
                    rulesParam.setRuleEmbargos(newRuleEmbargos);
                    newBillRulesParamsList.add(rulesParam);
                }
                String newJsonRule = JSON.toJSONString(newBillRulesParamsList);
                //将新的jsonRule跟新到数据库
                oaSysBillRuleMapper.updateByRuleId(ruleId, newJsonRule);
            }
        }
        return null;
    }

    @Override
    public Rv getStationList(PriceParam priceParam) {
        List<OaSysBillSpecialSceneRule> specialSceneRules = null;
        if (CheckUtil.objIsNotEmpty(priceParam) && CheckUtil.objIsNotEmpty(priceParam.getAreaId(), priceParam.getCallType())) {
            Integer cityId = priceParam.getAreaId().intValue();
            Integer callType = priceParam.getCallType();
            Integer stationKind = -1;
            //接机送机转换成 机场
            if (callType == 3 || callType == 4) {
                stationKind = 1;
            }
            //接站送站转换成 火车站
            if (callType == 5 || callType == 6) {
                stationKind = 2;
            }
            //根据cityId和stationKind查询是否有多机场多火车站
            QueryWrapper<OaSysBillSpecialSceneRule> sceneRuleQueryWrapper = new QueryWrapper<>();
            sceneRuleQueryWrapper.eq(OaSysBillSpecialSceneRule.CITY_CODE, cityId);
            sceneRuleQueryWrapper.eq(OaSysBillSpecialSceneRule.STATION_KIND, stationKind);
            specialSceneRules = oaSysBillSpecialSceneRuleMapper.selectList(sceneRuleQueryWrapper);
        }
        return Rv.wrap(EErrorCode.success, specialSceneRules);
    }

    /**
     * 新版本添加费用模板
     * @param billRulesParam
     * @return
     */
    /*public Rv addNewCharterPriceDetails(NewBillRulesParam billRulesParam){

    }*/

}
